/*kvan
 *2014.5.12
 */
package com.taxsystem.likaifan.taxcompute;
import java.util.concurrent.ExecutionException;

import org.json.JSONException;
import org.json.JSONObject;

import com.main.taxsystem.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class IndividualIncomeTaxCompute extends Activity {

	public final static String RECI_COAST = "sw.system.HTTUTIL";
	private TextView show_result;
	

	final int MY_NOTICE = 0x111;
	final int NAV_SETTING = 0x112;
	final int ACCOUNT_SETTING = 0x113;
	final int INDIVIDUAL_SETTING = 0x114;
    private static final String[] type = {"工资、薪金所得","财产转让所得","个体工商户生产、经营","企事业单位的承包、承租经营","利息、股息、红利、偶然和其他",
"报酬、稿酬、特许权使用、财产租赁"};

    private String result ="{\"data\":{\"tax\": \"123.4\"}}";

    
	private Spinner income_type;
	private EditText income_amount;
	private EditText insurance_amount;
	private EditText original_value;
	private EditText reasonable_cost;
	
	private int current;
	
	private String income_type_str;
	
	private Button button;
	
	private String requestUrl;
	
	private TableRow ia;
	private TableRow ov;
	private TableRow rc;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.individual_income_tax_compute);
		
		income_type = (Spinner) findViewById(R.id.income_type);
		income_amount = (EditText) findViewById(R.id.income_amount);
		insurance_amount = (EditText) findViewById(R.id.insurance_amount);
		original_value = (EditText) findViewById(R.id.original_value);
		reasonable_cost = (EditText) findViewById(R.id.reasonable_cost);
		ia = (TableRow) findViewById(R.id.ia);
		ov = (TableRow) findViewById(R.id.ov);
		rc = (TableRow) findViewById(R.id.rc);
		
		income_type.setPrompt("请选择收入类型"); 
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, type);
	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);   
	    income_type.setAdapter(adapter);  

	    income_type.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){

	           public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
	           {

	       		//Toast msg = Toast.makeText(individualIncomeTaxCompute.this, "选择的是"+m_arr[arg2], Toast.LENGTH_LONG);
	       		//msg.show();
	        	   //income_type_str = type[arg2];
	        	   //arg0.setVisibility(View.VISIBLE);
	        	   switch(arg2)
	        	   {
	        	   case 0:
	        		   ia.setVisibility(0);
	        		   rc.setVisibility(8);
	        		   ov.setVisibility(8);
	        		   current = 0;
	        		   break;
	        	   case 1:
	        		   ia.setVisibility(8);
	        		   rc.setVisibility(0);
	        		   ov.setVisibility(0);
	        		   current = 1;
	        		   break;
	        	   default:
	        		   ia.setVisibility(8);
	        		   rc.setVisibility(8);
	        		   ov.setVisibility(8);
	        		   current = 2;
	        	   }

	           }

	           public void onNothingSelected(AdapterView<?> arg0)
	           {
	               //
	           }

	       });


	    

		// 注册广播
		IntentFilter filter = new IntentFilter(RECI_COAST);
		BroadcastReceiver myrecive = new MyRecive();
		registerReceiver(myrecive, filter);
		Thread thread = new Thread() 
		{

			@Override
			public void run() 
			{
				Intent intent = new Intent(RECI_COAST);
				try {
					//获取服务器返回的信息
					String reslut = com.taxsystem.tool.HttUtil
							.getRequest(requestUrl);
					intent.putExtra("weatherinfo", reslut);
					//发送广播
					sendBroadcast(intent);
				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (ExecutionException e) {
					e.printStackTrace();
				}
			}

		};
	}

	private boolean isNoEmpty()
	{
		if(income_amount.length()<1)
			return false;
		if(current == 0)
		{
			if(insurance_amount.length()<1)
				return false;
		}
		if(current == 1)
		{	
			if(original_value.length()<1 || reasonable_cost.length()<1)
			return false;
		}
		
		return true;
	}
	
	
	public void check(View view)
	{
		if(isNoEmpty())
		{
		 String url = null;
	     if(current == 0)
	     {
	    	url = "http://mtaxtest.duapp.com/calculator/personaltax?typeid=1&income="+income_amount.getText()+"&socialsecurity="+insurance_amount.getText()+"&intrinsicalproperty=5000&expense=200";
	    	//url="http://mtaxtest.duapp.com/calculator/personaltax?typeid=1&income=1000&socialsecurity=22&intrinsicalproperty=5000&expense=200";
	    	//String result = null;
	    	//Log.d("url",url);
			/*
	    	try {
				result = HttUtil.getRequest(url);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			*/
	    	 Intent intent = new Intent(this, IndividualIncomeTaxResult.class);
			 intent.putExtra("result", result);
			 startActivity(intent);
	     }
	     else if(current == 1)
	     {
	    	 //url = "http://mtaxtest.duapp.com/calculator/personaltax?typeid=1&income="+income_amount.getText()+"&intrinsicalproperty="+original_value.getText()+"&expense="+reasonable_cost.getText();
	    	 //Log.d("url",url);
	    	 //String result = null;
			/*
	    	 try {
				result = HttUtil.getRequest(url);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			*/
	    	 Intent intent = new Intent(this, IndividualIncomeTaxResult.class);
			 intent.putExtra("result", result);
			 startActivity(intent);
	     }	 
	     else
	     {
	    	 //url = "http://mtaxtest.duapp.com/calculator/personaltax?typeid=1&income="+income_amount.getText();
	    	//String result = null;
			/*
	    	 try {
				result = HttUtil.getRequest(url);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			*/
			 Intent intent = new Intent(this, IndividualIncomeTaxResult.class);
			 intent.putExtra("result", result);
			 startActivity(intent);
	     }
			
		 //requestUrl = "收入类型为："+income_type_str+"\n收入数额为："+income_amount.getText()+ "\n查询结果:未拿到接口";
		}
		else
		{
			Toast msg = Toast.makeText(IndividualIncomeTaxCompute.this, "请把信息填写完整再查询", Toast.LENGTH_LONG);
			msg.show();
		}
	}
	
	public void reset(View view)
	{
		Intent intent = new Intent(this, IndividualIncomeTaxCompute.class);
		startActivity(intent);
		this.finish();
	}
	
	// 定义BroadcastReceiver 接收返回的信息
	private class MyRecive extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			try {
				//获取JSONObject对象
				JSONObject jsonobject = new JSONObject(
						intent.getStringExtra("weatherinfo"));
				JSONObject jsoncity = new JSONObject(
						jsonobject.getString("weatherinfo"));
				show_result.setText("城市:" + jsoncity.getString("city") + "\t"
						+ "日期:" + jsoncity.getString("date_y") + "\n" + "今天天气:"
						+ jsoncity.getString("temp1") + "\t"
						+ jsoncity.getString("weather1")+"\t"+jsoncity.getString("wind1"));
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.activity_mian, menu);
		menu.add(0, MY_NOTICE, 0, "我的通知");
		menu.add(0, NAV_SETTING, 0, "导航设置");
		menu.add(0, ACCOUNT_SETTING, 0, "账号设置");
		menu.add(0, INDIVIDUAL_SETTING, 0, "个性化设置");
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem mi)
	{
		
		
		//判断单击的是哪个菜单项，并针对性的作出响应。
		switch (mi.getItemId())
		{
			case android.R.id.home:
				this.finish();
				break;
			case MY_NOTICE:
				Log.d("hehe","heh");
				Toast toast1 = Toast.makeText(getApplicationContext(),
					     "MY_NOTICE", Toast.LENGTH_SHORT);
				toast1.show();
				break;
			case NAV_SETTING:
				Toast toast2 = Toast.makeText(getApplicationContext(),
					     "NAV_SETTING", Toast.LENGTH_SHORT);
				toast2.show();;
				break;
			case ACCOUNT_SETTING:
				Toast toast3 = Toast.makeText(getApplicationContext(),
					     "ACCOUNT_SETTING", Toast.LENGTH_SHORT);
				toast3.show();
				break;
			case INDIVIDUAL_SETTING:
				Toast toast4 = Toast.makeText(getApplicationContext(),
					     "INDIVIDUAL_SETTING", Toast.LENGTH_SHORT);
				toast4.show();
				break;

		}
		return true;
	}
}
